package cn.shop.dys.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.bean.Topic;
import cn.shop.dys.constant.CommonConstant;

/**
 * Created by liulu on 2016/4/24.
 */
public class CxkbAdapter extends BaseAdapter{

    public Context mcontext;
    private  List<Topic> topics;

    public CxkbAdapter(Context mcontext, List<Topic> topics) {
    this.mcontext = mcontext;
        this.topics = topics;
    }

    @Override
    public int getCount() {
        return topics == null?0:topics.size();
    }

    @Override
    public Topic getItem(int position) {
        return topics.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new ViewHolder();
            view = convertView.inflate(mcontext, R.layout.cxkb_item, null);
            viewHolder.iv_pic = (ImageView) view.findViewById(R.id.iv_cxkb);
            viewHolder.name = (TextView) view.findViewById(R.id.tv_cxkb);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        Topic topic = getItem(position);
        viewHolder.name.setText(topic.getName());
        String url = CommonConstant.HOST+topic.getPic();
        Glide.with(mcontext)
                .load(url)
                .fitCenter()
                .crossFade()
                .into(viewHolder.iv_pic);
        return view;
    }
    class ViewHolder{
        private TextView name;
        private ImageView iv_pic;
    }
}
